package bb.lanxing.lib.devices.utils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import android.os.ParcelUuid;

import bb.lanxing.lib.devices.api.DeviceApplication;
import bb.lanxing.lib.devices.api.DeviceManager;
import bb.lanxing.lib.devices.api.DeviceProvider;
import bb.lanxing.lib.devices.api.SmartDevice;
import bb.lanxing.lib.devices.ble.BLEAttributes;
import bb.lanxing.lib.devices.core.utils.DeviceHelper;
import bb.lanxing.lib.devices.utils.scan.ParseBluetoothAdData;

//import static bb.lanxing.lib.devices.utils.BLEAttributes.BLE_HEART_RATE_SERVICE;

public class DeviceUtils {
    private static final String CADENCE_APPEARANCE = "85 04";
    public static final int HEART_RATE_DEVICE_LOCATION_CHEST = 1;
    public static final int HEART_RATE_DEVICE_LOCATION_EARLOBE = 5;
    public static final int HEART_RATE_DEVICE_LOCATION_FINGER = 3;
    public static final int HEART_RATE_DEVICE_LOCATION_FOOT = 6;
    public static final int HEART_RATE_DEVICE_LOCATION_HAND = 4;
    public static final int HEART_RATE_DEVICE_LOCATION_OTHER = 0;
    public static final int HEART_RATE_DEVICE_LOCATION_WRIST = 2;
    private static final String IGSPORT_C61 = "c61";
    private static final String IGSPORT_SPD61 = "spd61";
    private static final String MAGENE_CADENCE = "gemini 200";
    private static final String XOSS_VORTEX = "xoss_v";
    private static final String TAG = "DeviceUtils";

    public static boolean shouldConnectToDualModeDevice(int i, String str, DeviceApplication deviceApplication) {
        if (!isDualModeDevice(str)) {
            return false;
        }
        DeviceProvider deviceProvider = null;
        if (i == 19) {
            boolean contains = str.toLowerCase().contains("vor_s");
            List<SmartDevice> devicesByType = DeviceHelper.getDevicesByType(19);
            if (devicesByType != null && !devicesByType.isEmpty()) {
                SmartDevice smartDevice = null;
                SmartDevice smartDevice2 = null;
                for (SmartDevice smartDevice3 : devicesByType) {
                    if (CadenceUtils.hasWheelRevolution(smartDevice3.getFlags())) {
                        smartDevice = smartDevice3;
                    } else if (CadenceUtils.hasCrankRevolution(smartDevice3.getFlags())) {
                        smartDevice2 = smartDevice3;
                    }
                }
                if (contains && smartDevice != null && DeviceHelper.isConnected(smartDevice.getAddress())) {
                    return false;
                }
                if (!contains && smartDevice2 != null && DeviceHelper.isConnected(smartDevice2.getAddress())) {
                    return false;
                }
            }
        }
        DeviceManager deviceManager = deviceApplication != null ? deviceApplication.getDeviceManager() : null;
        if (deviceApplication != null) {
            deviceProvider = deviceApplication.getDeviceProvider();
        }
        if (deviceManager == null || deviceProvider == null) {
            return false;
        }
        int i2 = 0;
        for (SmartDevice smartDevice4 : deviceProvider.getDevices()) {
            if (isDualModeDevice(smartDevice4.getName()) && deviceManager.isConnected(smartDevice4.getAddress())) {
                i2++;
            }
        }
        return i2 < 2;
    }

    public static boolean isXossHeartRateBeltDevice(String str, byte[] bArr) {
        boolean vRet = false;
        ArrayList<UUID> uuidList = ParseBluetoothAdData.Companion.parse(bArr).getUUIDs();
        if (bArr != null && bArr.length >= 4
                && containsUUID(uuidList, UUID.fromString(BLEAttributes.BLE_HEART_RATE_SERVICE))) {
            if (!str.toLowerCase().contains("xoss_")) {
                vRet = true;
            }
        }
        return vRet;
    }

    public static boolean isXossNav(String str) {
        return str != null && (str.toLowerCase().contains("nav") || str.toLowerCase().contains("dfu_nav"));
    }

    public static boolean isDualModeDevice(String str) {
        boolean vRet = false;
        if (str == null) {
            return false;
        }
        String lowerCase = str.toLowerCase();
        if (lowerCase.matches("xoss [sc]-\\d+")
                || MAGENE_CADENCE.equals(lowerCase)
                || IGSPORT_C61.equals(lowerCase)
                || IGSPORT_SPD61.equals(lowerCase)
                || lowerCase.contains(XOSS_VORTEX)) {
            vRet = true;
        }
        return vRet;
    }

    public static boolean isCadenceDevice(byte[] bArr) {
        boolean vRet = false;
        if (bArr != null && bArr.length >= 4
                && containsUUID(ParseBluetoothAdData.Companion.parse(bArr).getUUIDs(), UUID.fromString(BLEAttributes.BLE_CADENCE_SERVICE))) {
            vRet = true;
        }
        return vRet;
    }

    public static boolean isCadence01Device(String str, byte[] bArr) {
        return isCadenceDevice(bArr) && (str.toLowerCase().startsWith("cad01-"));
    }

    public static boolean isCadenceDevice(SmartDevice smartDevice) {
        boolean vRet = false;
        if (smartDevice == null) {
            return false;
        }
        if (smartDevice.getType() == SmartDevice.TYPE_VORTEX_CADENCE
                || smartDevice.getType() == SmartDevice.TYPE_CADENCE
                || smartDevice.getType() == SmartDevice.TYPE_CADENCE01
                || smartDevice.getType() == SmartDevice.TYPE_Q1) {
            vRet = true;
        }
        return vRet;
    }

    public static boolean isQi(String str) {
        return str != null && str.toLowerCase().startsWith("qi");
    }

    public static boolean isSprintDevice(String str, byte[] bArr) {
        boolean z;
        byte[] extractScanRecord = new byte[0];
        if (bArr == null || bArr.length < 4) {
            return false;
        }
        try {
            extractScanRecord = BleUtils.extractScanRecord(bArr, 255);
        } catch (Exception unused) {
        }
        if (extractScanRecord != null) {
            z = new String(extractScanRecord, 0, extractScanRecord.length).startsWith("XZS1");
            String lowerCase = str == null ? str.toLowerCase() : "";
            return !z || lowerCase.toLowerCase().startsWith("sprint") || lowerCase.matches("^dfu_[\\w]+");
        }
        return false;
    }

    public static boolean isHeartRateBeltDevice(String str, byte[] bArr) {
        boolean vRet = false;
        ArrayList<UUID> uuidList = ParseBluetoothAdData.Companion.parse(bArr).getUUIDs();
        if (bArr != null && bArr.length >= 4
                && containsUUID(uuidList, UUID.fromString(BLEAttributes.BLE_HEART_RATE_SERVICE))) {
            if (str.contains("xoss") || str.contains("hrm")) {
                vRet = true;
            }
        }
        return vRet;
    }

    public static boolean isHeartRateDevice(String str, byte[] bArr) {
        boolean vRet = false;
        ArrayList<UUID> uuidList = ParseBluetoothAdData.Companion.parse(bArr).getUUIDs();
        if (bArr != null && bArr.length >= 4
                && containsUUID(uuidList, UUID.fromString(BLEAttributes.BLE_HEART_RATE_SERVICE))) {
                vRet = true;
        }
        return vRet;
    }
    public static boolean isHeartRate01Device(String str, byte[] bArr) {
        return isHeartRateDevice(str, bArr) && (str.toLowerCase().startsWith("hr01-"));
    }

    public static boolean isPowerRateDevice(String str, byte[] bArr) {
        boolean vRet = false;
        ArrayList<UUID> uuidList = ParseBluetoothAdData.Companion.parse(bArr).getUUIDs();
        if (bArr != null && bArr.length >= 4
                && containsUUID(uuidList, UUID.fromString(BLEAttributes.BLE_CYCLING_POWER_SERVICE))) {
            if(containsUUID(uuidList, UUID.fromString(BLEAttributes.BLE_CADENCE_SERVICE))){
                vRet = false;
            }else{
                vRet = true;
            }
        }
        return vRet;
    }

    public static boolean isBikeTrainerDevice(String str, byte[] bArr) {
        boolean vRet = false;
        ArrayList<UUID> uuidList = ParseBluetoothAdData.Companion.parse(bArr).getUUIDs();
        if (bArr != null && bArr.length >= 4
                && containsUUID(uuidList, UUID.fromString(BLEAttributes.BLE_CYCLING_POWER_SERVICE))
                && containsUUID(uuidList, UUID.fromString(BLEAttributes.BLE_CADENCE_SERVICE))) {
            vRet = true;
        }
        if((str.toLowerCase().contains("vanrysel-ht"))
            ||(str.toLowerCase().contains("xoss_x2-test"))
            ||(str.toLowerCase().contains("xoss_vor_c-test"))){
                vRet = true;
        }

        return vRet;
    }

    public static boolean isIndoorBikeDevice(String str, byte[] bArr) {
        boolean vRet = false;
        ArrayList<UUID> uuidList = ParseBluetoothAdData.Companion.parse(bArr).getUUIDs();
        if (bArr != null && bArr.length >= 4
                && containsUUID(uuidList, UUID.fromString(BLEAttributes.BLE_FTMS_SERVICE))) {
            if((str.toLowerCase().contains("fs-ld cx50"))
                ||(str.toLowerCase().contains("domyos-bike"))
                ||(str.toLowerCase().contains("xoss-test"))
                ){
                vRet = true;
            }
        }
        return vRet;
    }

    public static boolean isRowerDevice(String str, byte[] bArr) {
        boolean vRet = false;
        ArrayList<UUID> uuidList = ParseBluetoothAdData.Companion.parse(bArr).getUUIDs();
        if (bArr != null && bArr.length >= 4
                && containsUUID(uuidList, UUID.fromString(BLEAttributes.BLE_FTMS_SERVICE))) {
            if(str.toLowerCase().contains("dkn-rw700")
              ||(str.toLowerCase().contains("xoss-test"))
              ||(str.toLowerCase().contains("pm5"))){
                vRet = true;
            }
        }
        return vRet;
    }

    public static boolean isCrossTrainerDevice(String str, byte[] bArr) {
        boolean vRet = false;
        ArrayList<UUID> uuidList = ParseBluetoothAdData.Companion.parse(bArr).getUUIDs();
        if (bArr != null && bArr.length >= 4
                && containsUUID(uuidList, UUID.fromString(BLEAttributes.BLE_FTMS_SERVICE))) {
            if(str.toLowerCase().contains("dkn-el540")){
                vRet = true;
            }
            if(str.toLowerCase().contains("xoss-test")){
                vRet = true;
            }
        }
        return vRet;
    }

    protected static final char[] hexArray = "0123456789ABCDEF".toCharArray();

    public static String bytesToHex(byte[] bArr) {
        char[] cArr = new char[(bArr.length * 3) - 1];
        for (int i = 0; i < bArr.length; i++) {
            int i2 = bArr[i] & 255;
            int i3 = i * 3;
            char[] cArr2 = hexArray;
            cArr[i3] = cArr2[i2 >>> 4];
            cArr[i3 + 1] = cArr2[i2 & 15];
            if (i != bArr.length - 1) {
                cArr[i3 + 2] = '-';
            }
        }
        return new String(cArr);
    }

    public static boolean isTreadmillAdvertising(String str, byte[] bArr) {
        boolean vRet = false;

        if (bArr == null || bArr.length < 4) {
            return false;
        }

        if(ParseBluetoothAdData.Companion.parse(bArr).getServicedataByte() != null) {
            if(appearanceEquals(bytesToHex(ParseBluetoothAdData.Companion.parse(bArr).getServicedataByte()), "26-18-01-01-00")){
                vRet = true;
            }
        }
        return vRet;
    }

    public static boolean isCrossTrainerAdvertising(String str, byte[] bArr) {
        boolean vRet = false;

        if (bArr == null || bArr.length < 4) {
            return false;
        }

        if(ParseBluetoothAdData.Companion.parse(bArr).getServicedataByte() != null) {
            if(appearanceEquals(bytesToHex(ParseBluetoothAdData.Companion.parse(bArr).getServicedataByte()), "26-18-01-02-00")){
                vRet = true;
            }
        }
        return vRet;
    }

    public static boolean isStepClimberAdvertising(String str, byte[] bArr) {
        boolean vRet = false;

        if (bArr == null || bArr.length < 4) {
            return false;
        }

        if(ParseBluetoothAdData.Companion.parse(bArr).getServicedataByte() != null) {
            if(appearanceEquals(bytesToHex(ParseBluetoothAdData.Companion.parse(bArr).getServicedataByte()), "26-18-01-04-00")){
                vRet = true;
            }
        }
        return vRet;
    }

    public static boolean isStairClimberAdvertising(String str, byte[] bArr) {
        boolean vRet = false;

        if (bArr == null || bArr.length < 4) {
            return false;
        }

        if(ParseBluetoothAdData.Companion.parse(bArr).getServicedataByte() != null) {
            if(appearanceEquals(bytesToHex(ParseBluetoothAdData.Companion.parse(bArr).getServicedataByte()), "26-18-01-08-00")){
                vRet = true;
            }
        }
        return vRet;
    }

    public static boolean isRowerAdvertising(String str, byte[] bArr) {
        boolean vRet = false;

        if (bArr == null || bArr.length < 4) {
            return false;
        }

        if(ParseBluetoothAdData.Companion.parse(bArr).getServicedataByte() != null) {
            if(appearanceEquals(bytesToHex(ParseBluetoothAdData.Companion.parse(bArr).getServicedataByte()), "26-18-01-10-00")){
                vRet = true;
            }
        }
        return vRet;
    }

    public static boolean isIndoorBikeAdvertising(String str, byte[] bArr) {
        boolean vRet = false;

        if (bArr == null || bArr.length < 4) {
            return false;
        }

        if(ParseBluetoothAdData.Companion.parse(bArr).getServicedataByte() != null) {
            if(appearanceEquals(bytesToHex(ParseBluetoothAdData.Companion.parse(bArr).getServicedataByte()), "26-18-01-20-00")){
                vRet = true;
            }
        }
        return vRet;
    }

    public static boolean isDiscoveryDevice(byte[] bArr) {
        byte[] extractScanRecord;
        if (bArr == null || bArr.length < 4 || (extractScanRecord = BleUtils.extractScanRecord(bArr, 255)) == null) {
            return false;
        }
        return new String(extractScanRecord).toLowerCase().contains("discovery");
    }

    public static boolean isXossGDevice(String str) {
        if (str != null) {
            return str.toLowerCase().contains(SmartDevice.XOSS_G_KEY_NAME) || str.toLowerCase().contains("dfu_g");
        }
        return false;
    }

    public static boolean isXossX2Pro(String str) {
        return str != null && (str.toLowerCase().contains("xoss_x2p") || str.toLowerCase().contains("dfu_x2p"));
    }

    public static boolean isV3DFUDevice(String str) {
        return isXossGDevice(str) || isXossNav(str) || isXossX2Pro(str);
    }

    public static boolean isV3DFUDeviceNeedDisInfo(String str) {
        return isXossNav(str) || isXossX2Pro(str);
    }

    public static boolean isBici(byte[] bArr) {
        if (bArr == null || bArr.length < 11) {
            return false;
        }
        byte[] bArr2 = new byte[2];
        System.arraycopy(bArr, 11, bArr2, 0, 2);
        return new String(bArr2).toLowerCase().equals(SmartDevice.BICI_KEY_NAME);
    }

    public static boolean isLightDevice(String str, byte[] bArr) {
        if (bArr == null || bArr.length < 4) {
            return false;
        }
        return appearanceEquals(ParseBluetoothAdData.Companion.parse(bArr).getAppearance(), "0580");
    }

    public static boolean containsUUID(ParcelUuid[] parcelUuidArr, UUID uuid) {
        boolean z = false;
        for (ParcelUuid parcelUuid : parcelUuidArr) {
            if (parcelUuid.getUuid().compareTo(uuid) == 0) {
                z = true;
            }
        }
        return z;
    }

    public static boolean containsUUID(ArrayList<UUID> arrayList, UUID uuid) {
        Iterator<UUID> it = arrayList.iterator();
        boolean z = false;
        while (it.hasNext()) {
            if (it.next().compareTo(uuid) == 0) {
                z = true;
            }
        }
        return z;
    }

    public static boolean isDfuDevice(SmartDevice smartDevice) {
        return smartDevice.getName().toLowerCase().contains("dfu");
    }

    public static boolean appearanceEquals(String str, String str2) {
        if (str == null || str2 == null) {
            return false;
        }
        return str.toLowerCase().contains(str2.toLowerCase());
    }
}
